// ---------------------------------------------------------------------
//
// -- 大厅界面
// -- author £
//
// ---------------------------------------------------------------------

const UIManager = require('UIManager'),
    CommonInfo = require('CommonInfo');

cc.Class({
    extends: cc.Component,

    properties: {
        content: cc.Node,
        btnStart: cc.Node,
        btnRank: cc.Node,
        btnMoreGame: cc.Node,
        gameAdLayer: cc.Node,
        btnSoundOn: cc.Node,
        btnSoundOff: cc.Node,
        btnRule: cc.Node,
        middleGroup: cc.Node,
        titleGroup: cc.Node,
        bottomGroup: cc.Node,
        btnShare: cc.Node,
        btnShareGroup: cc.Node,
        resultView: require('ResultView'),
        scoreView: require('ScoreView'),
        rulePanel: require('RulePanel'),
        lbVesion: cc.Label,
        numRCoin: require('BaseNum'),
        shareRCoinPanel: require('ShareRCoinPanel'),
        btnShareRCoin: cc.Node,
        btnWorldRank: cc.Node,

        pArr: {
            default: [],
            type: cc.ParticleSystem
        }
    },

    // LIFE-CYCLE CALLBACKS:

    ctor: function () {
        this._mediator = require('LobbyMediator');
    },

    onLoad () {
        let UIManager = require('UIManager');
        
        this.btnMoreGameSp = this.btnMoreGame.getComponent(cc.Sprite);
        this.switchSoundBtn(false);
        this.content.height = UIManager.getRealH();
        this.content.y = - UIManager.getRootY();
        this._initAdLayerY = this.gameAdLayer.y;
        this.lbVesion.string = 'ver ' + require('AppInfo').version;
        this.numRCoin.updateNum('0a5');

        let m = this._mediator;

        this.btnStart.on('click', m.toGameScene, m);
        this.resultView.btnRestart.on('click', m.toGameScene, m);
        this.btnMoreGame.on('click', this._mediator.moreGameHandler, this._mediator);
        this.btnRank.on('click', this.friendRankHandler, this);
        this.resultView.btnRank.on('click', this.friendRankHandler, this);
        this.resultView.btnChallenge.on('click', m.shareScoreHandler, m);
        this.btnSoundOn.on('click', this._mediator.soundOnHandler, this._mediator);
        this.btnSoundOff.on('click', this._mediator.soundOffHandler, this._mediator);
        this.btnRule.on('click', this.ruleHandler, this);
        this.btnShare.on('click', m.shareHandler, m);
        this.btnShareGroup.on('click', m.shareHandler, m);
        this.btnShareRCoin.on('click', m.introRCoinHandler, m);
        this.btnWorldRank.on('click', m.worldRankHandler, m);
        this.shareRCoinPanel.btnShare.on('click', m.shareRCoinHandler, m);
    },

    onShow: function () {
        if (!CommonInfo.showResult) {
            this.switchParticles(true);
            this.middleGroup.active = true;
            this.titleGroup.active = true;
            this.scoreView.hide();
        } else  {
            this.switchParticles(false);
            this.middleGroup.active = false;
            this.titleGroup.active = false;
            this.resultView.show();
        }

        this.gameAdLayer.y = - UIManager.getRealH() * 0.6;
    },

    onHide () {
        this.switchParticles(false);
        this.resultView.hide();
        this.gameAdLayer.y = this._initAdLayerY - UIManager.getDeltaH();
    },

    start () {
        this._mediator.register(this);
    },

    showRCoinPanel: function () {
        this.shareRCoinPanel.show();
    },

    hideRCoinPanel: function () {
        this.shareRCoinPanel.hide();
    },

    updateRCoin: function (cur, total, shareGet) {
        this.numRCoin.updateNum(cur + 'a' + total);
        this.shareRCoinPanel.updateRCoin(cur, total, shareGet);
    },

    switchSoundBtn: function (flag) {
        this.btnSoundOff.active = !flag;
        this.btnSoundOn.active = flag;
    },

    switchParticles: function (flag) {
        let len = this.pArr.length;

        for (let i = 0; i < len; ++i) {
            flag ?
                this.pArr[i].resetSystem() :
                this.pArr[i].stopSystem();
            this.pArr[i].node.active = flag;
        }
    },

    stopResultUpdate: function () {
        this.resultView.stop();
    },

    refreshMoreGameIcon: function (index, url) {
        require('TextureManager').getTexture(
            'common',
            url,
            'cm_icon_' + index,
            this.btnMoreGameSp
        );
    },

    //----------------------------------------------------------



    //----------------------------------------------------------

    friendRankHandler: function () {
        this.resultView.stop();
        this._mediator.friendRankHandler();
    },

    ruleHandler: function () {
        this.rulePanel.show();
    },
    // update (dt) {
        
    // },
});
